package com.tc168.cp306.b06.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.maning.mndialoglibrary.MProgressDialog;
import com.tc168.cp306.b06.MyApplication;
import com.tc168.cp306.b06.R;
import com.tc168.cp306.b06.activity.CuoMatterActivity;
import com.tc168.cp306.b06.adaptre.Answer_userAdapter;
import com.tc168.cp306.b06.bean.UserAnswerbean;
import com.tc168.cp306.b06.gen.UserAnswerbeanDao;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class HistoryFragment extends Fragment {


    @BindView(R.id.rv)
    RecyclerView anRv;
    Unbinder unbinder;
    @BindView(R.id.aaaaa)
    LinearLayout aaaaa;

    public HistoryFragment() {
        // Required empty public constructor
    }

    private List<UserAnswerbean> mdata;
    private Answer_userAdapter answer_itemAdapter;
    private UserAnswerbeanDao topticbeanDao;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_history, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        topticbeanDao = MyApplication.getmDaoSessiontwo().getUserAnswerbeanDao();


        MProgressDialog.showProgress(getActivity());


        new Handler().postDelayed(new Runnable() {

            @Override

            public void run() {
                MProgressDialog.dismissProgress();
            }
        }, 1000);


        mdata = new ArrayList<>();
        answer_itemAdapter = new Answer_userAdapter(getActivity(), mdata);

        anRv.setAdapter(answer_itemAdapter);
        anRv.setLayoutManager(new LinearLayoutManager(getActivity()));

        answer_itemAdapter.setOnItemClickListener(new Answer_userAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                Intent intent = new Intent(getActivity(), CuoMatterActivity.class);
                intent.putExtra("num", postion);
                startActivity(intent);
            }
        });

        List<UserAnswerbean> list = topticbeanDao.queryBuilder().where(UserAnswerbeanDao.Properties.Istrue.eq(false)).list();
        mdata.addAll(list);
        answer_itemAdapter.notifyDataSetChanged();

        if (mdata.size()==0){
            aaaaa.setVisibility(View.VISIBLE);
        }else {
            aaaaa.setVisibility(View.GONE);

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
